package com.teach;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

public class List01 {

	public static void main(String[] args) {
		List<Long> list = new ArrayList<Long>();
		
		System.out.println("list="+list);
		//从collection继承的方法
		list.add(1L);
		list.add(2L);
		list.addAll(Arrays.asList(3L,2L,1L));
		//...
		System.out.println("list="+list);
		list.add(0,4L);
		list.add(5,5L);
//		list.add(50,5L);//IndexOutOfBoundsException
		System.out.println("list="+list);

		System.out.println("list.get(0)="+list.get(0));
		System.out.println("list.get(5)="+list.get(5));
//		System.out.println("list.get(5)="+list.get(-1));//ArrayIndexOutOfBoundsException
//		System.out.println("list.get(5)="+list.get(100));//ArrayIndexOutOfBoundsException

		System.out.println("list.remove(0)="+list.remove(0));
		System.out.println("list="+list);
		System.out.println("list.remove(3)="+list.remove(3));
		System.out.println("list="+list);
		System.out.println("list.remove(3L)="+list.remove(3L));//删除某个元素,从collection继承的方法
		System.out.println("list="+list);
		
		List<Long> list1 = list.subList(1, 3);
		System.out.println("list1="+list1);
		
		list.set(3, 3L);
		System.out.println("list="+list);

		System.out.println("list.indexOf(2L)="+list.indexOf(2L));//equals
		System.out.println("list.indexOf(4L)="+list.indexOf(4L));
		
		System.out.println("list.lastIndexOf(2L)="+list.lastIndexOf(2L));//equals
		System.out.println("list.lastIndexOf(4L)="+list.lastIndexOf(4L));
		
		System.out.println("list.contains(2L)="+list.contains(2L));//equals
		System.out.println("list.contains(4L)="+list.contains(4L));
		
		Random r = new Random();
		for(int i=0;i<10;i++) {
			list.add((long)(r.nextInt(50)+1));
		}
		System.out.println("list="+list);
		
		//升序
		list.sort(new Comparator<Long>() {

			@Override
			public int compare(Long o1, Long o2) {
				return (int)(o1-o2);
			}
		});
		System.out.println("list="+list);

		//降序
		list.sort((a,b)->(int)(b-a));
		System.out.println("list="+list);
		
		//乱序
		list.sort((a,b)->r.nextInt());
		System.out.println("list="+list);
		
		list1 = new ArrayList<Long>();
		list1.add(1L);
		list1.add(2L);
		list1.add(3L);
		System.out.println("-------------------list1:");
		for(Long item : list1) {
			System.out.println(item);
		}
		System.out.println("-------------------list1:");
		Iterator<Long> it = list1.iterator();
		while(it.hasNext()) {
			Long item = it.next();
			System.out.println(item);
		}
		System.out.println("-------------------list1:");
		list1.forEach((item)->{
			System.out.println(item);
		});
		System.out.println("-------------------list1:");
		for(int i=0;i<list1.size();i++) {
			System.out.println(list1.get(i));
		}
		System.out.println("关于循环和删除问题");
		System.out.println("目标删除集合中的偶数");
		List<Long> list2 = new ArrayList<Long>();
		list2.addAll(Arrays.asList(3L,4L,4L,5L,6L,7L));
		System.out.println("list2:"+list2);
		
		list2.removeIf(item->item%2==0);
		System.out.println("list2:"+list2);
		
		list2.clear();
		list2.addAll(Arrays.asList(3L,4L,4L,5L,6L,7L));
		it = list2.iterator();
		while(it.hasNext()) {
			Long item = it.next();
			if(item % 2 == 0) {
				it.remove();
			}
		}
		System.out.println("list2:"+list2);
		
		System.out.println("错误的演示:逻辑问题");
		list2.clear();
		list2.addAll(Arrays.asList(3L,4L,4L,5L,6L,7L));
		for(int i=0;i<list2.size();i++) {
			if(list2.get(i) % 2 == 0) {
				list2.remove(i);
			}
		}
		System.out.println("list2:"+list2);
		
		System.out.println("错误演示:集合已修改异常");
//		list2.clear();
//		list2.addAll(Arrays.asList(3L,4L,4L,5L,6L,7L));
////		int index=0;
//		for(Long item : list2) {
//			if(item % 2 == 0) {
////				list2.remove(index);//ConcurrentModificationException
//				list2.remove(item);//ConcurrentModificationException
//			}
////			index++;
//		}
		
	}
}
